<template>
<div>
  <!-- 结果列表 -->
  <el-card>
    <el-table :data="onlineList" stripe style="width: 100%">
      <el-table-column label="#" width="80">
        <template slot-scope="scope">
          {{(searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="180">
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button @click="openViewListUI(scope.row.id)" type="primary" icon="el-icon-thumb" size="mini" circle></el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>


  <el-dialog title="在线详情" :visible.sync="dialogListVisible">
    <el-table :data="dialogOnlineList">
      <el-table-column property="username" label="用户名" width="150"></el-table-column>
      <el-table-column property="ip" label="登录IP" width="200"></el-table-column>
      <el-table-column property="lastLoginTime" label="最近登录时间">
        <template slot-scope="scope">
          <span>{{scope.row.lastLoginTime|datefmt('YYYY-MM-DD HH:mm:ss')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button @click="handleLogoutToken(scope.row.token)" type="primary" icon="el-icon-close" size="mini" circle></el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>

  <!-- 分页组件 -->
  <el-pagination
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
    :current-page="searchModel.pageNo"
    :page-sizes="[5, 10, 20, 50]"
    :page-size="searchModel.pageSize"
    layout="total, sizes, prev, pager, next, jumper"
    :total="total"
  >
  </el-pagination>
</div>
</template>

<script>
import {deleteUserById, getOnlineUserById, getOnlineUserList, onlineLogout} from "@/api/user";
import {filter} from "minimatch";

export default {
  name: "onlineUser",
  data() {
    return {
      dialogListVisible: false,
      title: "",
      total: 0,
      searchModel: {
        pageNo: 1,
        pageSize: 10,
      },
      onlineList: [],
      dialogOnlineList: [],
    }
  },

  created() {
    this.getOnlineList();
  },

  methods: {
    getOnlineList(){
      getOnlineUserList(this.searchModel).then(response => {
        this.onlineList = response.data.rows;
        this.total = response.data.total;
      })
    },
    openViewListUI(id){
      getOnlineUserById(id).then(response => {
        this.dialogOnlineList = response.data;
        this.dialogListVisible = true;
      })
    },
    handleLogoutToken(token){
      this.$confirm(`您确认剔除该在线用户?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        onlineLogout(token).then(response =>{
          this.$message({
            type: 'success',
            message: "操作成功"
          });
          this.dialogListVisible = false;
          this.getOnlineList();
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消剔除'
        });
      });
    },
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize;
      this.getUserList();
    },
    handleCurrentChange(pageNo) {
      this.searchModel.pageNo = pageNo;
      this.getUserList();
    },
  }
}
</script>

<style scoped>

</style>
